package com.sw.small_world.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.alibaba.mobileim.IYWLoginService;
import com.alibaba.mobileim.YWAPI;
import com.alibaba.mobileim.YWIMKit;
import com.alibaba.mobileim.YWLoginParam;
import com.alibaba.mobileim.channel.event.IWxCallback;
import com.sw.small_world.R;
import com.sw.small_world.application.SWApplication;
import com.sw.small_world.utils.CommonUtils;
import com.sw.small_world.utils.ToastUtil;

import butterknife.Bind;

/**
 * Created by Administrator on 2017/3/24.
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.login_phone_number)
    EditText loginPhoneNumber;
    @Bind(R.id.login_code)
    EditText loginCode;
    @Bind(R.id.login)
    Button login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initIMKit();
//        if (ShareUtils.getBoolean(ShareUtils.LOGIN_STATE, true)) {
//
//        } else {
//            setContentView(R.layout.activity_login);
//            ButterKnife.bind(this);
//        }
    }

    private void loginIM() {
        //开始登录
        String userid = "testpro1";
        String password = "taobao1234";
        IYWLoginService loginService = SWApplication.getiMKit().getLoginService();
        YWLoginParam loginParam = YWLoginParam.createLoginParam(userid, password);
        loginService.login(loginParam, new IWxCallback() {

            @Override
            public void onSuccess(Object... arg0) {
                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                finish();
            }

            @Override
            public void onProgress(int arg0) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onError(int errCode, String description) {
                //如果登录失败，errCode为错误码,description是错误的具体描述信息
                ToastUtil.show("登录失败" + description);
            }
        });
    }

    private void initIMKit() {
        //此实现不一定要放在Application onCreate中
        final String userid = "testpro1";
        //此对象获取到后，保存为全局对象，供APP使用
        //此对象跟用户相关，如果切换了用户，需要重新获取
        YWIMKit mIMKit = YWAPI.getIMKitInstance(userid, SWApplication.APP_KEY);
        SWApplication.setiMKit(mIMKit);
        loginIM();
    }

    @Override
    public void onClick(View v) {

    }
}
